package cn.com.lyj6851.modules.wechat;

import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.logging.aop.AopLog;
import cn.com.lyj6851.modules.mp.domain.YdyWechatLive;
import cn.com.lyj6851.modules.mp.service.YdyWechatLiveService;
import cn.com.lyj6851.modules.mp.service.dto.UpdateGoodsDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatLiveDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatLiveQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

@AllArgsConstructor
@Api(tags = "wxlive管理")
@RestController
@RequestMapping("/api/ydyWechatLive")
public class YdyWechatLiveController {

    private final YdyWechatLiveService ydyWechatLiveService;
    private final IGenerator generator;


    @AopLog("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@per.check('admin','WechatLive:list')")
    public void download(HttpServletResponse response, YdyWechatLiveQueryCriteria criteria) throws IOException {
        ydyWechatLiveService.download(generator.convert(ydyWechatLiveService.queryAll(criteria), YdyWechatLiveDto.class), response);
    }

    @GetMapping
    @AopLog("查询wxlive")
    @ApiOperation("查询wxlive")
    @PreAuthorize("@per.check('admin','WechatLive:list')")
    public ResponseEntity<Object> getYdyWechatLives(YdyWechatLiveQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(ydyWechatLiveService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @PostMapping
    @AopLog("新增wxlive")
    @ApiOperation("新增wxlive")
    @PreAuthorize("@per.check('admin','WechatLive:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody YdyWechatLive resources){
        return new ResponseEntity<>(ydyWechatLiveService.saveLive(resources),HttpStatus.CREATED);
    }


    @PostMapping("/addGoods")
    @AopLog("添加商品")
    @ApiOperation("添加商品")
    @PreAuthorize("@per.check('admin','ydyWechatLive:add')")
    public ResponseEntity<Object> addGoods(@Validated @RequestBody UpdateGoodsDto resources){
        return new ResponseEntity<>(ydyWechatLiveService.addGoods(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @AopLog("修改wxlive")
    @ApiOperation("修改wxlive")
    @PreAuthorize("@per.check('admin','ydyWechatLive:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody YdyWechatLive resources){
        ydyWechatLiveService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @AopLog("删除wxlive")
    @ApiOperation("删除wxlive")
    @PreAuthorize("@per.check('admin','ydyWechatLive:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            ydyWechatLiveService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @ApiOperation("同步数据")
    @GetMapping("/synchro")
    public ResponseEntity<Object> synchroWxOlLive() {
        ydyWechatLiveService.synchroWxOlLive();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
